package com.tsystems.clientEmployee.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.TitledBorder;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.tsysstems.clientEmployee.ClientEmployee;



public class MainPanel extends JPanel {

	private static final long serialVersionUID = -2753908159857600873L;
	
	Logger logger = LoggerFactory.getLogger(getClass());
	

	private JPanel customPanel;
	

	
	
	ClientEmployee clientPassenger = ClientEmployee.getInstance();

	
	private JPanel menuPanel;

	private JButton stationManagementBtn;

	private JButton trainsManagementBtn;

	private JButton passangersManagementBtn;
	
	private JButton tteManagementBtn;

	public MainPanel() {
		super(new BorderLayout());
		
		initComponents();
		initListeners();	

		
		
		
		updatePanel();
	} 
	
	

	private void initComponents() {
		
		
		
		JPanel ticketTablePanel = createMenuPanel();
		customPanel = createGreetingsPanel();
		add(ticketTablePanel, BorderLayout.WEST);
		add(customPanel, BorderLayout.CENTER);
		
	}

	private JPanel createGreetingsPanel() {	
        JPanel rightPanel = new JPanel(new GridBagLayout());
       rightPanel.setBorder(new TitledBorder("Панель приветствия"));
		
		
		JPanel result = new JPanel(new FlowLayout(FlowLayout.CENTER));
		
		

		
		result.add(new JLabel("Добро пожаловать в систему Train System !!"));
		
		rightPanel.add(result, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, 
				GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, 
				new Insets(-5, 0, 0, 0), 0, 0));	
		
		return rightPanel;
	}
	
	
	private JPanel createMenuPanel() {
		JPanel result = new JPanel(new GridBagLayout());
		result.setBorder(new TitledBorder("Меню"));

		menuPanel = new JPanel(new GridBagLayout());
		result.add(menuPanel, new GridBagConstraints(0, 0,
				1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
				GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
		
		
	
		JPanel panel = new JPanel(new GridBagLayout());
		stationManagementBtn = new JButton("Управление станциями");
		trainsManagementBtn = new JButton("Управление поездами");
		passangersManagementBtn = new JButton("Управление пассажирами");
		tteManagementBtn = new JButton("Управление расписанием");
		
		panel.add(stationManagementBtn, new GridBagConstraints(0, 0, 1, 1, 1.0,
				0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
				new Insets(0, 0, 7, 0), 0, 0));
		panel.add(trainsManagementBtn, new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
				new Insets(0, 0, 7, 0), 0, 0));
		panel.add(passangersManagementBtn, new GridBagConstraints(0, 2, 1, 1, 1.0, 0.0,
				GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
				new Insets(0, 0, 7, 0), 0, 0));
		panel.add(tteManagementBtn, new GridBagConstraints(0, 3, 1, 1, 1.0, 0.0,
				GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
				new Insets(0, 0, 7, 0), 0, 0));
		
		result.add(panel, new GridBagConstraints(0, 2, 1, 1, 0.0, 1.0,
				GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
				new Insets(0, 7, 7, 7), 0, 0));
		
		return result;
	}
	
	private void initListeners() {
		
		
		stationManagementBtn.addActionListener(new ActionListener() {			
			public void actionPerformed(ActionEvent e) {
	
				remove(customPanel);
				customPanel=new StationManagementPanel();
				add(customPanel, BorderLayout.CENTER);
				updateUI();
				
			}
		});
		
		passangersManagementBtn.addActionListener(new ActionListener() {		
			public void actionPerformed(ActionEvent e) {
				remove(customPanel);
				customPanel=new PassengersManagementPanel();
				add(customPanel, BorderLayout.CENTER);
				updateUI();
			}
		});
		
		trainsManagementBtn.addActionListener(new ActionListener() {		
			public void actionPerformed(ActionEvent e) {
				remove(customPanel);
				customPanel=new TrainsManagementPanel();
				add(customPanel, BorderLayout.CENTER);
				updateUI();
			}
		});
		tteManagementBtn.addActionListener(new ActionListener() {		
			public void actionPerformed(ActionEvent e) {
				remove(customPanel);
				customPanel=new TTEManagementPanel();
				add(customPanel, BorderLayout.CENTER);
				updateUI();
			}
		});
	}
	

	
	public void updatePanel() {
	
	}
	
	public static void showFrame() {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				String login = ClientEmployee.getInstance().getLogin();
				
				JFrame frame = new JFrame("Train System: login as '" + login + "'");
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.setSize(new Dimension(1100, 500));
				frame.setLocationRelativeTo(null);
				final MainPanel mainPanel = new MainPanel();
		
				
				frame.setContentPane(mainPanel);
				
				frame.addWindowListener(new WindowAdapter() {
					@Override
					public void windowClosing(WindowEvent e) {
						ClientEmployee.getInstance().close();
					}
				});
				
				frame.setVisible(true);
			}
		});
	}
}
